/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package listcontrol;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.UUID;
import util.CompleteFile;
import util.HashMD5;

/**
 *
 * @author gravena
 */
public class LocalFileList {

    private long version;
    //private ArrayList<CompleteFile> fList;
    private final HashMap<HashMD5, CompleteFile> fileList;
    public static UUID localUuid;

    public LocalFileList() {
        this.version = 0;
        this.fileList = new HashMap<>();

        this.rebuildList();
    }

    public void resetList() {
        this.fileList.clear();
    }

    public boolean searchFile(byte[] hash) {
        return this.fileList.containsKey(new HashMD5(hash));
    }

    public CompleteFile retrieveFile(byte[] hash) {
        return this.fileList.get(new HashMD5(hash));
    }

    public boolean addCompleteFile(CompleteFile file) {
        CompleteFile add = this.fileList.put(file.getHashMD5Wrapper(), file);
        if (add != null) {
            return true;
        } else {
            return false;
        }

    }

    public boolean removeCompleteFile(String file) {
        Collection<CompleteFile> values = this.fileList.values();
        Iterator<CompleteFile> it = values.iterator();
        while (it.hasNext()) {
            CompleteFile cur = it.next();
            if (cur.getName().equals(file)) {
                values.remove(cur);
                return true;
            }
        }
        return false;
    }

    public void rebuildList() {
        java.io.File dir = new File(util.File.sharePath);
        File[] listFiles = dir.listFiles();
        fileList.clear();
        for (int i = 0; i < listFiles.length; i++) {
            if (listFiles[i].isFile()) {
                try {
                    CompleteFile entry = new CompleteFile(listFiles[i].getName());
                    fileList.put(entry.getHashMD5Wrapper(), entry);
                } catch (IOException ex) {
                    System.err.println("Erro ao adicionar arquivo local a lista local!\n" + ex.getMessage());
                }
            }
        }
    }

    public long getVersion() {
        return version;
    }

    public void setVersion(long version) {
        this.version = version;
    }

    public void incVersion() {
        this.version++;
    }

    public UUID getUUID() {
        return LocalFileList.localUuid;
    }

    public String getUUIDString() {
        String u = LocalFileList.localUuid.toString();
        StringBuilder v = new StringBuilder(u.length());

        int i = 0;

        while (i < u.length()) {
            if (u.charAt(i) != '-') {
                v.append(u.charAt(i));
            }
            i++;
        }

        return v.toString();
    }

    public CompleteFile searchFile(String name) {
        Collection<CompleteFile> values = this.fileList.values();
        Iterator<CompleteFile> it = values.iterator();
        while (it.hasNext()) {
            CompleteFile cur = it.next();
            if (cur.getName().equals(name)) {
                return cur;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        Collection<CompleteFile> values = this.fileList.values();
        Iterator<CompleteFile> it = values.iterator();
        String r = "Version: " + this.version + "\n";
        while (it.hasNext()) {
            CompleteFile cur = it.next();
            r += cur;
            r += "\n\n";
        }
        return r;
    }

    public void setUuid(UUID uuid) {
        LocalFileList.localUuid = uuid;
    }

    public int getNumberOfFiles() {
        return this.fileList.size();
    }

    public CompleteFile getFile(int i) {
        Collection<CompleteFile> values = this.fileList.values();
        Iterator<CompleteFile> it = values.iterator();
        int j = 0;
        while (it.hasNext()) {
            CompleteFile cur = it.next();
            if (i == j) {
                return cur;
            }
            j++;
        }
        return null;
    }

    public Collection<CompleteFile> getFileCollection() {
        return this.fileList.values();
    }
}
